export function didMount() {
    console.log("「页面 JS」：当前页面地址 " + location.href);
    debugger
    const self = this;
    const elements = {
        punishmentCount: self.$$("container_6aelur"),
        ordinaryProcedure: self.$$("container_dnu90k"),
        summaryProcedure: self.$$("container_8kwn1c"),
        decideType1: self.$$("text_8z8ken"),
        decideType2: self.$$("text_gcm45d"),
        decideType3: self.$$("text_fd0zj3"),
        decideType4: self.$$("text_dw7ijp"),
        decideType5: self.$$("text_9gd89m"),
        decideType6: self.$$("text_1oegym"),
        decideType7: self.$$("text_hkfhcw"),
        decideType8: self.$$("text_41ci0m"),
    };
    let listTitle = self.$$("text_bkttpg")

    const setButtonStyle = (element) => {
        if (element && element.$el) {
            element.$el.style.cursor = 'pointer';
            element.$el.style.transition = 'all 0.2s ease';
            // 添加悬停效果
            element.$el.addEventListener('mouseenter', () => {
                element.$el.style.transform = 'scale(1.02)';
                element.$el.style.boxShadow = '0 2px 8px rgba(0,0,0,0.1)';
            });
            element.$el.addEventListener('mouseleave', () => {
                element.$el.style.transform = 'scale(1)';
                element.$el.style.boxShadow = 'none';
            });
        }
    };

    Object.values(elements).forEach(setButtonStyle);


    let pageTableList = self.$$("page_widget_list_6ndvbc");

    // 按钮点击处理函数
    const handleButtonClick = (buttonName, event) => {
        debugger
        const titleMap = {
            punishmentCount: '行政处罚预警清单',
            ordinaryProcedure: '普通程序预警清单',
            summaryProcedure: '简易程序预警清单',
            decideType1: document.getElementById("entityValidityName").textContent + '预警清单' || '主体合法性预警清单',
            decideType2: document.getElementById("procedureStandardName").textContent + '预警清单' || '程序规范性预警清单',
            decideType3: document.getElementById("evidenceQualityName").textContent + '预警清单' || '证据质量预警清单',
            decideType4: document.getElementById("documentArchiveName").textContent + '预警清单' || '文书档案预警清单',
            decideType5: document.getElementById("discretionExerciseName").textContent + '预警清单' || '裁量行使预警清单',
            decideType6: document.getElementById("enforcementImpactName").textContent + '预警清单' || '执法影响预警清单',
            decideType7: document.getElementById("infoDisclosureName").textContent + '预警清单' || '信息公开预警清单',
            decideType8: document.getElementById("integrityDisciplineName").textContent + '预警清单' || '廉洁与纪律预警清单'
        };


        if (listTitle && titleMap[buttonName]) {
            // console.log(`${buttonName}按钮被点击`);
            listTitle.$$setValue(titleMap[buttonName]);
            // 点击对列表进行过滤
            const isProcedureButton = ['punishmentCount', 'ordinaryProcedure', 'summaryProcedure'].includes(buttonName);
            const isDecideButton = buttonName.startsWith('decideType');

            let bizIdValue = null;
            let ruleIdValue = null;
            if (isProcedureButton) {
                if(buttonName === 'punishmentCount'){
                    bizIdValue = '1,2'
                } else if(buttonName === 'ordinaryProcedure'){
                    bizIdValue = '2'
                }else {
                    bizIdValue = '1'
                }
            }else if (isDecideButton) {
                const findRuleId = (element) => {
                    if (!element) return null;
                    if (element.dataset && element.dataset.ruleId) return element.dataset.ruleId;
                    return findRuleId(element.parentElement);
                };
                ruleIdValue = findRuleId(event.target);
            }


            const countryValue = self.$$("text_input_6iltos").cValue;
            const provinceValue = self.$$("text_input_e7ln66").cValue;
            const cityValue = self.$$("text_input_1x0udy").cValue;
            const districtValue = self.$$("text_input_55q4pw").cValue;
            const streetValue = self.$$("text_input_dd3dav").cValue;
            const communityValue = self.$$("text_input_ayzvd0").cValue;

            let result = {
                ruleId: ruleIdValue,
                countryId: countryValue,
                provinceId: provinceValue,
                cityId: cityValue,
                districtId: districtValue,
                streetId: streetValue,
                community: communityValue,
                bizId: bizIdValue
            };
            filterList(pageTableList, result)
        }
    };

    Object.keys(elements).forEach(key => {
        if (key !== 'listTitle' && elements[key] && elements[key].$el) {
            elements[key].$el.addEventListener("click", (event) => handleButtonClick(key, event));
        }
    });
}


function filterList(pageTableList, result) {

    if (pageTableList.dataEngine && pageTableList.dataEngine.state) {
        // 给列表组件设置参数，参数名meter_id为要过滤列表中的列名，后面为参数值，取自树节点参数
        pageTableList.$children[0].$$setExtraParams({
            ruleId: result.ruleId,
            countryId: result.countryId,
            provinceId: result.provinceId,
            cityId: result.cityId,
            districtId: result.districtId,
            streetId: result.streetId,
            communityId: result.community,
            bizId: result.bizId
        })
        // 找到列表部件中的表格组件元素
        let tableComp = pageTableList.dataEngine.state.getElement("table_ycrya");
        if (tableComp) {
            //执行列表刷新
            tableComp.$$refreshData();
        }
    }
}
